//
//  AddressViewController.m
//  SeaChat
//
//  Created by 潘强 on 16/6/13.
//  Copyright © 2016年 潘强. All rights reserved.
//

#import "AddressViewController.h"
#import "SearchVC.h"
#import "BuddyCell.h"
#import "ConversationViewController.h"
static NSString *reuse = @"reuse";
@interface AddressViewController ()<UITableViewDataSource,UITableViewDelegate,EMClientDelegate,EMContactManagerDelegate>



@end

@implementation AddressViewController

- (void)dealloc
{
    [[EMClient sharedClient] removeDelegate:self ];
    [_contactList release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title  = @"通讯录";

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(searchBuddy)];

    [[EMClient sharedClient] addDelegate:self delegateQueue:nil];
    [[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];

    self.contactList =  [[EMClient sharedClient].contactManager getContactsFromDB];
    NSLog(@"%@",self.contactList);

#warning 强调buddylist没有值的情况 1.第一次登录 2.自动登录还有没有完成
    if (self.contactList.count == 0) {
        //数据库没有好友记录
        EMError *error = nil;
        self.contactList = [[EMClient sharedClient].contactManager getContactsFromServerWithError:&error];
        NSLog(@"%@",self.contactList);
        if (error) {
            NSLog(@"%@",error);
        }
    }

    //buddylist 在自动登录成功后才有结果.
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.tableView registerClass:[BuddyCell class] forCellReuseIdentifier:reuse];
    // Do any additional setup after loading the view.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 3;
            break;
        case 1:
            return self.contactList.count;
            break;

        default:
            break;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 15;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{



    BuddyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                cell.headV.image = [UIImage imageNamed:@"newFriends@2x"];
                cell.NameLabel.text = @"新的朋友";
                break;
            case 1:
                cell.headV.image = [UIImage imageNamed:@"groupPublicHeader@2x"];
                cell.NameLabel.text = @"群聊";
                break;
            case 2:
                cell.headV.image = [UIImage imageNamed:@"group_header@2x"];
                cell.NameLabel.text = @"聊天室";
                break;
            default:
                break;
        }

    }else{
        NSString *buddy = [self.contactList objectAtIndex:indexPath.row];
        cell.NameLabel.text = buddy;
        cell.headV.image = [UIImage imageNamed:@"IMG_0659.jpg"];
//EMClient
    }


    return cell;
}

- (void)didReceiveAgreedFromUsername:(NSString *)aUsername{
   self.contactList =    [[EMClient sharedClient].contactManager getContactsFromServerWithError:nil];
    NSLog(@"%@",self.contactList);
    [self.tableView reloadData];
}

-(void)didReceiveAddedFromUsername:(NSString *)aUsername{

    self.contactList =    [[EMClient sharedClient].contactManager getContactsFromServerWithError:nil];
    NSLog(@"%@",self.contactList);
    [self.tableView reloadData];

}

- (void)didReceiveDeletedFromUsername:(NSString *)aUsername{
    self.contactList =    [[EMClient sharedClient].contactManager getContactsFromDB];
    NSLog(@"%@",self.contactList);

    [self.tableView reloadData];
    NSLog(@"222");
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ConversationViewController *conversation = [ConversationViewController new];
    conversation.hidesBottomBarWhenPushed = YES;
    conversation.userName = self.contactList[indexPath.row];
    [self.navigationController pushViewController:conversation animated:1];
    [conversation release];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSString *userName =        self.contactList[indexPath.row];
                EMError *error = [[EMClient sharedClient].contactManager deleteContact:userName];
                if (!error) {
                    NSLog(@"删除成功");
                }
                NSLog(@"%@",error);

    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return HEIGHT/13;

}
-(void)searchBuddy{
    SearchVC *SVC = [[SearchVC alloc]init];
    [self.navigationController pushViewController:SVC animated:1];
    
}


@end
